import { message } from 'ant-design-vue';
import { getAnalysisDataByKeys } from '@/api/goods'
import { exportFile } from '@/api/api'
import { uploadToken } from '@/api/api'

const globalData = {
  uploadParams: {
    actionUrl: 'https://up-z0.qiniup.com/', // 七牛上传
    headers: {
      authorization: 'authorization-text'
    },
    uploadHost: 'https://img.bzhz.jnbygroup.com/'
  }

}
export default globalData

const uploadImgHost = 'https://img.bzhz.jnbygroup.com/'
export { uploadImgHost }

export function getBase64 (img, callback) {
  const reader = new FileReader()
  reader.addEventListener('load', () => callback(reader.result))
  reader.readAsDataURL(img)
}

// 上传图片获取token
export function fetchUploadToken () {
  return uploadToken().then(res => {
    if (res.code === 0) {
      return res.data
    } else {
      message.error(res.msg)
    }
  }).catch(err => {
    console.log('fetchUploadToken:' + err)
  })
}

// 导出
export function fetchExportFileId (param, serviceId) {
  let data = {
    params: JSON.stringify(param),
    serviceId: serviceId
  }
  return exportFile(data).then(res => {
    if (res.success) {
      return res.result
    } else {
      message.warning(res.message)
    }
  })
}

export function queryExportSuccess (timer, result, seconds, callback) {
  let startTime = new Date().getTime();
  timer = setInterval(() => {
    if (new Date().getTime() - startTime > seconds) {
      clearInterval(timer)
      message.warning('导出超时!')
      callback('done')
    } else {
      let params = {
        keys: result
      }
      return getAnalysisDataByKeys(params).then(res => {
        // console.log(res.data.success)
        if (res.code == 0) {
          if (res.data.success) {
            message.success('导出成功！')
            // window.open(res.data.url)
            clearInterval(timer)
            callback('done', res.data.url)
          }
        } else {
          message.warning(res.msg)
          clearInterval(timer)
          callback('done')
        }
      })
    }
  }, 2000)
}

// 导入
export function queryUploadSuccess (timer, result, seconds, callback) {
  let startTime = new Date().getTime();
  let that = this
  timer = setInterval(() => {
    if (new Date().getTime() - startTime > seconds) {
      clearInterval(timer)
      message.warning('导入超时!')
      callback('warning')
    } else {
      let params = {
        keys: result
      }
      getAnalysisDataByKeys(params).then(res => {
        if (res.code == 0) {
          if (res.data.success) {
            message.success(res.msg)
            if (res.data.url) {
              window.open(res.data.url)
            }
            clearInterval(timer)
            callback('done')
          }
        } else {
          message.warning(res.msg)
          clearInterval(timer)
          callback('warning')
        }
      })
    }
  }, 2000)
}

// 截取url
export function getQueryString (name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
  var r = window.location.search.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}
export function getUrlParam (paraName) {
  let url = document.location.toString();
  let arrObj = url.split("?");

  if (arrObj.length > 1) {
    let arrPara = arrObj[1].split("&");
    let arr;

    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=");

      if (arr != null && arr[0] == paraName) {
        return arr[1];
      }
    }
    return "";
  } else {
    return "";
  }
}
